from sklearn.feature_extraction import image
import numpy as np
from sklearn.feature_extraction import image


def loadData(filePath):
    f = open(filePath,'rb')
    data= []
    img =image.open(f)
    img = img.resize((112,112))
    m,n =img.size
    print(m,n)
    for i in range(m):
        for j in range(n):
            x,y,z =img.getpixel((i,j))
            data.append([x/256.0,y/256.0,z/256.0])
    f.close()
    return np.mat(data),m,n
imgData,row,col =loadData('c1.jpg')
from sklearn.cluster import SpectralClustering
label = SpectralClustering(n_clusters=3).fit_predict(imgData)
label=label.reshape([row,col])
pic_new = image.new("L",(row,col))
for i in range(row):
    for j in range(col):
        pic_new.putpixel((i,j),int(256/(label[i][j]+1)))
        pic_new.save("1000-see.jpg","JPEG")
from sklearn.cluster import  KMeans
km=KMeans(n_clusters=3)
label =km.fit_predict(imgData)
label=label.reshape([row,col])
pic_new = image.new("L",(row,col))
for i in range(row):
    for j in range(col):
        pic_new.putpixel((i,j),int(256/(label[i][j]+1)))
        pic_new.save("1000-se.jpg","JPEG")
